﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FreeWorkflow.Services;
using FreeWorkflow.DataModels;
using FreeWorkflow.Repository;

namespace FreeWorkflow.Activites
{
    public sealed class WorkflowTask : NativeActivity
    {
        public String ActivityId { get; set; }
        public OutArgument<Guid> WorkItemId { get; set; }
        public OutArgument<String> OutComeVariable { get; set; }

        [RequiredArgument]
        public InArgument<IUserInfo> UserInfo { get; set; }
        [RequiredArgument]
        public InArgument<String> Title { get; set; }
        protected override bool CanInduceIdle
        {
            get
            {
                return true;
            }
        }
        protected override void Execute(NativeActivityContext context)
        {
            try
            {
                WorkItemManager workitemManager = new WorkItemManager();

                WorkItem workitem = new WorkItem();
                workitem.Title = Title.Get(context);
                Guid TaskId = workitemManager.CreateTask(workitem,UserInfo.Get(context));
                WorkItemId.Set(context, workitem.Id);
                context.CreateBookmark(TaskId.ToString(), new BookmarkCallback(Callback));
            }
            catch (Exception ex)
            {
                throw;
            }
            //try
            //{
            //    //TaskHelper task_helper = new TaskHelper();
            //    //WorkflowProperties wfp = context.GetExtension<WorkflowProperties>();
            //    //PGD_DAL.Task task = task_helper.CreateTask(TaskDefinitionID.Get(context), UserType, UserInfo.Get(context), wfp.WorkflowInstanceID);

            //    //TaskID.Set(context, task.ID);
            //    //context.CreateBookmark(task.ID.ToString(), new BookmarkCallback(Callback));
            //}
            //catch (Exception ex)
            //{
            //    LogEx.Log(ex);
            //}
        }

        private void Callback(NativeActivityContext context, Bookmark bookmark, object state)
        {
            //if (TaskHelper.IsTaskCompleted(TaskID.Get(context)))
            //{
            //    if (OutComeVariable != null)
            //        OutComeVariable.Set(context, state);
            //    return;
            //}
            //else
            //    context.CreateBookmark(TaskID.Get(context).ToString(), new BookmarkCallback(Callback));
        }
    }

    
}
